const mongoose = require('mongoose');

const CommentSchema = new mongoose.Schema({
        body: String,
        author: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User',
        },
        post: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Post',
        },
    },
    {
        timestamps: true,
    }
);

// Requires population of author
CommentSchema.methods.toJSON = function () {
    return {
        id: this._id,
        body: this.body,
        createdAt: this.createdAt,
        author: this.author.toProfileJSON(),
    };
};

mongoose.model('Comment', CommentSchema);
